﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using CharacterTrailLibrary.Factory;
using CharacterTrailLibrary.Interface;
using CharacterTrailLibrary.Persist.BLL.Model;
using CharacterTrailLibrary.Security;
using DevExpress.XtraEditors;

namespace CharacterTrailClient.Controls
{
    public partial class ProductBaseRepairEdit : DevExpress.XtraEditors.XtraUserControl
    {
        public bool AddSuccess { get; private set; }
        private readonly IProductPersist _productPersist = null;
        public readonly TProductBaseModel ProductBaseModel = new TProductBaseModel();
        public ProductBaseRepairEdit()
        {
            AddSuccess = false;
            _productPersist = InstanceFactory.GetProductPersist();
            ProductBaseModel.OperateMember = UserSession.CurrentUser.UserName;
            //维修
            ProductBaseModel.TranType = 2;
            InitializeComponent();

            this.productRepairHeaderEditRepair.Enabled = false;
            Binding();
        }

        private void Binding()
        {
            foreach (Control control in groupControlMain.Controls)
            {
                if (control is BaseEdit && control.Tag != null)
                {
                    var edit = (BaseEdit)control;
                    edit.DataBindings.Clear();
                    edit.DataBindings.Add("EditValue", ProductBaseModel, edit.Tag.ToString());
                }
            }
        }

        private void CmdAddBaseInfo_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void CmdChange_Click(object sender, EventArgs e)
        {
            this.CmdAddBaseInfo.Enabled = true;
            this.productRepairHeaderEditRepair.Enabled = false;
            this.memoRepairNote.Focus();
        }

        public IRepairHeaderViewControl FRepairHeaderViewControl { get; set; }

        public void Save()
        {
            try
            {
                ProductBaseModel.RowGuid = Guid.NewGuid().ToString("N");
                ProductBaseModel.InputDate = DateTime.Now;
                ProductBaseModel.InputId = UserSession.CurrentUser.UserId;
                ProductBaseModel.InputName = UserSession.CurrentUser.UserName;
                _productPersist.InsertProductBaseModel(ProductBaseModel);
                AddSuccess = true;
                this.labelControlMsg.Text = String.Format("提示信息：{0}", "新增成功！");

                this.CmdAddBaseInfo.Enabled = false;

                this.productRepairHeaderEditRepair.Enabled = true;
                this.productRepairHeaderEditRepair.SetFocus();
            }
            catch (Exception exception)
            {
                Message.MsgBox.ShowError(exception.Message);
            }
        }
    }
}
